

import Api from './api'
import Address from './address'

/**
 * 检验订单
 * @param product
 * @returns {Promise<*>}
 */
const placeOrder = async (product) => {
  let requestParams = {
    product: product
  }
  let res = await Api.netFetch(Address.placeOrder(), 'POST', requestParams, true)
  return res
};
/**
 * 下单
 * @param product
 * @returns {Promise<*>}
 */
const storeOrder =  async (product) => {
  let requestParams = {
    product: product
  }
  let res = await Api.netFetch(Address.order(), 'POST', requestParams, true)
  return res
};

const getOrderList = async (page , status) => {
  let url = Address.order() + Address.getPageParams("?", page) + `&status=${status}`;
  let res = await Api.netFetch(url);
  if (res && res.result) {
    let data = res.data.data
    data.forEach((item) => {
      let ex = {
        orderSn: item.order_sn,
        orderPrice: item.order_price,
        created: item.created_at,
        status: item.status,
      }
      item.ex = ex
    });
    return {
      data: data,
      result: res.result
    };
  }
};

const cancelOrder = async (id) => {
  let url = Address.cancelOrder(id);
  let res = await Api.netFetch(url);
  return res
};

export default {
  placeOrder,
  storeOrder,
  getOrderList,
  cancelOrder
}
